package net.lzzy.practicesapi1.repositories;

import net.lzzy.practicesapi1.entities.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.UUID;

/**
 * @author lyz
 */
public interface UserRepository extends JpaRepository<User, UUID> {
    User findUserByUserNameAndPassword(String userName,String password);
    Integer countByUserName(String userName);
    UserProjection findUsersByUserName(String userName);

    List<User> findAllByOrderByUpdateTimeDesc();

    /**
     * 分页获取用户数据
     * @param pageable 分页参数，包括页码和每页大小
     * @return 按分页大小返回User表记录
     */
    List<User> findAllByOrderByUpdateTimeDesc(Pageable pageable);

    /**
     * 返回User表中所有数据
     * @return 用户记录数
     */
    Integer countAllBy();
    List<User> findUsersByApplyTeacherOrderByUpdateTimeDesc(boolean apply);

    /**
     *
     * @param keyword 要查询的关键词
     * @return 模糊查询的结果集
     */
    @Query("select u from User u where u.userName like concat('%', :kw, '%')or " +
            "u.nickName like concat('%', :kw, '%') or u.email like concat('%', :kw, '%') or " +
            "u.phone like concat('%', :kw, '%') order by u.updateTime desc")
    List<User> findByQuery(@Param("kw") String keyword);
}
